package com.ruoyi.launch.controller;

import java.io.File;
import java.util.List;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.launch.util.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.launch.domain.EAdvertisement;
import com.ruoyi.launch.service.IEAdvertisementService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 智能柜分屏广告Controller
 *
 * @author ruoyi
 * @date 2024-11-27
 */
@RestController
@RequestMapping("/advertisement")
public class EAdvertisementController extends BaseController
{
    @Autowired
    private IEAdvertisementService eAdvertisementService;

    /**
     * 查询智能柜分屏广告列表
     */
    @RequiresPermissions("launch:advertisement:list")
    @GetMapping("/list")
    public TableDataInfo list(EAdvertisement eAdvertisement)
    {
        startPage();
        List<EAdvertisement> list = eAdvertisementService.selectEAdvertisementList(eAdvertisement);
        return getDataTable(list);
    }

    /**
     * 导出智能柜分屏广告列表
     */
    @RequiresPermissions("launch:advertisement:export")
    @Log(title = "智能柜分屏广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EAdvertisement eAdvertisement)
    {
        List<EAdvertisement> list = eAdvertisementService.selectEAdvertisementList(eAdvertisement);
        ExcelUtil<EAdvertisement> util = new ExcelUtil<EAdvertisement>(EAdvertisement.class);
        util.exportExcel(response, list, "智能柜分屏广告数据");
    }

    /**
     * 获取智能柜分屏广告详细信息
     */
    @RequiresPermissions("launch:advertisement:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(eAdvertisementService.selectEAdvertisementById(id));
    }

    /**
     * 新增智能柜分屏广告
     */
    @RequiresPermissions("launch:advertisement:add")
    @Log(title = "智能柜分屏广告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EAdvertisement eAdvertisement)
    {
        eAdvertisement.setStatus(1);
        return toAjax(eAdvertisementService.insertEAdvertisement(eAdvertisement));
    }

    /**
     * 修改智能柜分屏广告
     */
    @RequiresPermissions("launch:advertisement:edit")
    @Log(title = "智能柜分屏广告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EAdvertisement eAdvertisement)
    {
        return toAjax(eAdvertisementService.updateEAdvertisement(eAdvertisement));
    }

    /**
     * 删除智能柜分屏广告
     */
    @RequiresPermissions("launch:advertisement:remove")
    @Log(title = "智能柜分屏广告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(eAdvertisementService.deleteEAdvertisementByIds(ids));
    }
    @PostMapping("/upimg")
    public String uploadFile(MultipartFile video) throws Exception {
        if (video.isEmpty()){
            return "图片上传失败";
        }
        String originalFilename = video.getOriginalFilename();
        String ext="."+originalFilename.split("\\.")[1];
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String fileName=uuid+ext;
        //上传图片
        ApplicationHome applicationHome=new ApplicationHome(this.getClass());
        String pre=applicationHome.getDir().getParentFile().getAbsolutePath()+"\\ruoyi-modules\\ruoyi-launch\\src\\main\\resources\\images\\";
        String path=pre+fileName;
        try {
            video.transferTo(new File(path));
            return path;
        } catch (IOException e) {
            e.printStackTrace();
        }
      return "图片上传失败";
    }

    @RequiresPermissions("launch:advertisement:uploadVideo")
    @RequestMapping("/uploadVideo")
    public String uploadVideo(@RequestParam(value = "file", required = true) MultipartFile file) throws Exception {
        return UpdateUtil.uploadImage(file);
    }
    @RequiresPermissions("launch:advertisement:upPushStatus")
    @PutMapping("/upPushStatus/{id}")
    public AjaxResult upPushStatus(@PathVariable Integer id)
    {
        return toAjax(eAdvertisementService.upPushStatus(id));
    }
    @RequiresPermissions("launch:advertisement:downPushStatus")
    @PutMapping("/downPushStatus/{id}")
    public AjaxResult downPushStatus(@PathVariable Integer id)
    {
        return toAjax(eAdvertisementService.downPushStatus(id));
    }
    @RequiresPermissions("launch:advertisement:pushTime")
    @PutMapping("/pushTime")
    public AjaxResult pushTime(@RequestBody EAdvertisement eAdvertisement)
    {
        return toAjax(eAdvertisementService.pushTime(eAdvertisement));
    }

}
